<template>
  <div class="box">
    <div class="content">
      <div class="login-wrapper">
        <h1>厚溥外卖后台管理系统</h1>
        <div class="login-form">
          <div class="username form-item">
            <span>用户名</span>
            <el-icon
              :size="25"
              :color="color"
              style="position: absolute; top: 30px; left: 15px; z-index: 9"
            >
              <UserFilled />
            </el-icon>
            <input type="text" class="input-item" v-model="account" />
            <el-icon
              v-show="account"
              :size="22"
              :color="color"
              style="position: absolute; top: 32px; right: 12%; z-index: 9"
            >
              <CircleCheck />
            </el-icon>
          </div>
          <div class="password form-item">
            <span>密码</span>
            <el-icon
              :size="25"
              :color="color"
              class="icon"
              style="position: absolute; top: 30px; left: 15px; z-index: 9"
            >
              <Lock />
            </el-icon>
            <!-- .11 -->
            <input
              :type="showPasswordIcon ? 'text' : 'password'"
              class="input-item"
              v-model="password"
            />
            <el-icon
              v-show="password"
              :size="22"
              :color="color"
              style="position: absolute; top: 32px; right: 12%; z-index: 9"
            >
              <CircleCheck />
            </el-icon>
            <el-icon
              @click="togglePasswordVisibility"
              v-show="password && showPasswordIcon"
              :size="22"
              :color="color"
              style="position: absolute; top: 32px; right: 20%; z-index: 9"
            >
              <View />
            </el-icon>
            <el-icon
              @click="togglePasswordVisibility"
              v-show="password && !showPasswordIcon"
              :size="22"
              :color="color"
              style="position: absolute; top: 32px; right: 20%; z-index: 9"
            >
              <Hide />
            </el-icon>
          </div>
          <div class="form-forget">忘记账号/密码</div>
          <button class="login-btn" @click="toLogin()">登 录</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { getUsersCheckLogin } from "../api/index.js";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";

// 创建响应式变量
const account = ref("guest");
const password = ref("666");
const router = useRouter();
const showPasswordIcon = ref(true); // 控制显示哪个图标

function togglePasswordVisibility() {
  // 切换密码可见性
  showPasswordIcon.value = !showPasswordIcon.value;
}

const toLogin = async () => {
  try {
    const response = await getUsersCheckLogin({
      account: account.value,
      password: password.value,
    });
    console.log("🚀 ~ toLogin ~ response:", response);
    if (response.code === 0) {
      localStorage.setItem("t_k", JSON.stringify(response.token));
      ElMessage({
        message: "登录成功!",
        type: "success",
        duration: 1000,
      });
      // 假设 code 为 0 时登录成功，进行跳转
      setTimeout(() => {
        router.push("/dashboard/home"); // 替换为您的跳转路径
      }, 1000);
    } else {
      // 处理登录失败情况
      ElMessage({
        message: "用户名或密码错误",
        type: "error",
        duration: 1500,
      });
    }
  } catch (error) {
    console.error("登录请求失败:", error);
  }
};
</script>

<style scoped lang="less">
/*公共CSS*/
.form-forget {
  user-select: none;
  font-size: 15px;
  color: rgb(59, 22, 22);
  margin-bottom: 10px;
}
.box {
  user-select: none;
  width: 100vw;
  height: 100vh;
  background-color: rgb(255, 237, 225);
  color: #441c1c;
}
.box .content .login-wrapper h1 {
  font-size: 20px;
  text-align: center;
}
.box .content .login-wrapper .login-form .form-item {
  position: relative;
  margin: 10px 0;
}
.box .content .login-wrapper .login-form .form-item span {
  display: block;
  margin: 5px 20px;
  font-weight: 100;
}
.box .content .login-wrapper .login-form .form-item .input-item {
  width: 93%;
  border-radius: 40px;
  padding: 20px;
  box-sizing: border-box;
  font-size: 20px;
  font-weight: 200;
  text-indent: 8%;
  // position: relative;
}
.box .content .login-wrapper .login-form .form-item .input-item:focus {
  outline: none;
}
.box .content .login-wrapper .login-form .login-btn {
  width: 93%;
  border-radius: 40px;
  color: #fff;
  border: 0;
  font-weight: 100;
  margin-top: 10px;
  cursor: pointer;
}
.box .content .login-wrapper .divider {
  width: 100%;
  margin: 20px 0;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}
.box .content .login-wrapper .divider span:nth-child(1) {
  flex: 1;
}
.box .content .login-wrapper .divider span:nth-child(3) {
  flex: 1;
}
.box .content .login-wrapper .divider .line {
  display: inline-block;
  max-width: 30%;
  width: 30%;
}
.box .content .login-wrapper .divider .divider-text {
  vertical-align: middle;
  margin: 0px 20px;
  line-height: 0px;
  display: inline-block;
  width: 100px;
}
.box .content .login-wrapper .other-login-wrapper {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.box .content .login-wrapper .other-login-item {
  border: 1px solid rgb(214, 222, 228);
  padding: 10px;
  margin: 10px;
  cursor: pointer;
}

/*一般大于手机的尺寸CSS*/
@media (min-width: 767px) {
  .form-forget {
    font-size: 15px;
    color: rgb(165, 70, 58);
    margin-bottom: 10px;
  }
  .box {
    background-color: rgb(255, 123, 66);
  }
  .box .content {
    width: 60vw;
    height: 50vh;
    background: url("@/assets/bg-login.jpg") no-repeat;
    background-size: 90% 100%;
    position: absolute;
    right: 15%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 20px;
    background-color: #fff;
  }
  .box .content .login-wrapper {
    width: 27vw;
    position: absolute;
    right: 0%;
    top: 50%;
    transform: translateY(-50%);
  }
  .box .content .login-wrapper h1 {
    margin-right: 30px;
    text-align: center;
    font-size: 25px;
    color: rgb(231, 93, 19);
    margin-bottom: 40px;
  }
  .box .content .login-wrapper .login-form {
    margin: 10px 0;
  }
  .box .content .login-wrapper .login-form .form-item span {
    color: rgb(231, 93, 19);
  }
  .box .content .login-wrapper .login-form .form-item .input-item {
    height: 40px;
    width: 90%;
    border: 1px solid rgb(137, 76, 54);
  }
  .box .content .login-wrapper .login-form .login-btn {
    height: 50px;
    background-color: rgb(231, 93, 19);
    font-size: 20px;
    width: 90%;
  }
  .box .content .login-wrapper .divider .line {
    border-bottom: 1px solid rgb(255, 255, 255);
  }
  .box .content .login-wrapper .other-login-item {
    border-radius: 20px;
  }
  .box .content .login-wrapper .other-login-item img {
    width: 40px;
    height: 40px;
  }
}

@media (min-width: 1345px) {
  .box .content {
    width: 60vw;
    height: 55vh;
    background-size: 90% 100%;
    position: absolute;
    right: 15%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 20px;
    background-color: #fff;
  }
  .box .content .login-wrapper h1 {
    margin-right: 30px;
    text-align: center;
    font-size: 30px;
    color: rgb(231, 93, 19);
    margin-bottom: 40px;
  }
  .box .content .login-wrapper .login-form .form-item .input-item {
    height: 45px;
    width: 90%;
    border: 1px solid rgb(104, 43, 43);
  }
}
@media (min-width: 1500px) {
  .box .content {
    width: 55vw;
    height: 70vh;
    background-size: 90% 100%;
    position: absolute;
    right: 15%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 20px;
    background-color: #fff;
  }
  .box .content .login-wrapper h1 {
    margin-right: 30px;
    text-align: center;
    font-size: 34px;
    color: rgb(231, 93, 19);
    margin-bottom: 40px;
  }
}
/*手机端CSS*/
@media (max-width: 768px) {
  .box .content {
    width: 100vw;
    height: 100vh;
    background: url("./asset/login_bg_phone.png") no-repeat;
    background-size: 100% 100%;
    display: flex;
    align-items: flex-start;
    justify-content: center;
  }
  .box .content .login-wrapper {
    width: 70%;
    height: 60%;
    padding-top: 15%;
  }
  .box .content .login-wrapper h1 {
    font-size: 30px;
    color: #441c1c;
    margin-bottom: 40px;
  }
  .box .content .login-wrapper .login-form .form-item {
    margin: 10px 0;
  }
  .box .content .login-wrapper .login-form .form-item span {
    color: rgb(62, 15, 15);
  }
  .box .content .login-wrapper .login-form .form-item .input-item {
    height: 30px;
    border: 1px solid rgb(79, 49, 32);
    background-color: transparent;
    color: #814545;
  }
  .box .content .login-wrapper .login-form .login-btn {
    height: 45px;
    background-color: rgb(73, 37, 19);
    font-size: 16px;
  }
  .box .content .login-wrapper .divider .line {
    border-bottom: 1px solid #fff;
  }
  .box .content .login-wrapper .divider .divider-text {
    color: #fff;
  }
  .box .content .login-wrapper .other-login-item {
    border-radius: 15px;
  }
  .box .content .login-wrapper .other-login-item img {
    width: 35px;
    height: 35px;
  }
}
</style>
